package kr.totalcall.controls
{
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	
	import kr.totalcall.constant.TotalCallGlobals;
	import kr.totalcall.view.receive.CustLocationInfo;
	import kr.totalcall.view.receive.ReceiveForm;
	import kr.totalcall.view.receive.ReceiveInfoDetail;
	import kr.totalcall.view.receive.SearchCustomer;
	
	import spark.components.List;
	import spark.components.SkinnableContainer;
	import spark.events.IndexChangeEvent;

	/**
	 * 작업 기록
	 * 2012. 7. 4. / 이규광 / 최초 작성.
	 */
	/**
	 * 내용.
	 * @productversion Flex 4.5
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 10.2
	 * @author 이규광.
	 */
	public class TextInputCopyController
	{
		// copy menu..
		public var listCopy:List;
		
		private static const sign:Object = new Object();
		
		/**
		 * 생성자
		 */
		public function TextInputCopyController(obj:Object)
		{
			if(obj != sign){
				throw new Error("this is singleton class.." );
			}
		}
		
		
		
		private static var _controller:TextInputCopyController;
		
		public static function get controller():TextInputCopyController{
			if(!_controller){
				_controller = new TextInputCopyController(TextInputCopyController.sign);
			}
			return _controller;
		}
		
		
		
		public function initCopyMenu(copyMenu:List):void{
			if(listCopy ) return ;
			listCopy = copyMenu;
			listCopy.addEventListener(IndexChangeEvent.CHANGE, listCopy_changeHandler);
		}
		
		
		
		public function addTextInputCopyMenu(textInput:EventDispatcher):void{
			if(!textInput) return;
			textInput.addEventListener(MouseEvent.CLICK, text_clickHandler);
		}
		
		
		public function removeTextInputCopyMenu(textInput:EventDispatcher):void{
			if(!textInput) return;
			textInput.removeEventListener(MouseEvent.CLICK, text_clickHandler);
		}
		
		
		public var controlText:Object = null;
		
		protected function text_clickHandler(event:MouseEvent):void
		{
			if( TotalCallGlobals.main.bRightClick )
			{
				controlText = event.currentTarget;
				
				if(!listCopy) return;
				
				
				var textInputContainer:SkinnableContainer = null;
				
				if(event.currentTarget.parentDocument is ReceiveForm){
					textInputContainer = event.currentTarget.parentDocument;
				}else if(event.currentTarget.parentDocument is CustLocationInfo
					|| event.currentTarget.parentDocument is ReceiveInfoDetail ){
					textInputContainer = event.currentTarget.parentDocument.parentDocument;
				}else if(event.currentTarget.parentDocument is SearchCustomer){
					textInputContainer = event.currentTarget.parentDocument.parentDocument.parentDocument;
				}
				if(!textInputContainer) return;
				
				textInputContainer.addElement(listCopy);
				
				var point:Point = textInputContainer.globalToLocal(new Point(event.stageX, event.stageY));
				listCopy.x = point.x;
				listCopy.y = point.y - 30;
				listCopy.selectedIndex = -1;
				listCopy.visible = true;
				listCopy.setFocus();
				
			}
		}
		
		
		public var copyData:String = "";
		
		protected function listCopy_changeHandler(event:IndexChangeEvent):void
		{
			if( controlText != null )
			{
				if( event.newIndex == 0 )
				{
					ExternalInterface.call("setClipboardData", controlText.text);
					//Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, controlText.text);
					//copyData = controlText.text;
				}
				else if( event.newIndex == 1 )
				{
					controlText.text = ExternalInterface.call("getClipboardData");
					//var txt:String = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) as String;
					//controlText.text = txt;
				}
			}	
			controlText = null;
			listCopy.visible = false;
		}

		
	}
}